﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class CheckDataAccess : BaseDataAccess<Business.Entities.Check>
    {
        public override GMAC.ReportingSystem.Business.Entities.Check Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procCheckByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public CheckCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procCheckSelect", null);
            return ReadTable(dt) as CheckCollection;
        }

        protected override GMAC.ReportingSystem.Business.Entities.Check ReadRow(System.Data.DataRow row)
        {
            Check Check = null;
            if (row != null)
            {
                try
                {
                    Check = new Check();
                    Check.ID = (int)row["ID"];
                    Check.Amount = Decimal.Parse(row["Amount"].ToString());
                    Check.dDate = DateTime.Parse(row["dDate"].ToString());
                    Check.CheckNumber = row["CheckNumber"].ToString();
                    var bc = new BankDataAccess();
                    Check.Bank = bc.Get(row["DebitBankCode"].ToString());

                }
                catch (Exception)
                {
                    throw;
                }
            }
            return Check;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.Check> ReadTable(System.Data.DataTable table)
        {
            try
            {
                CheckCollection Checks = new CheckCollection();
                Check Check;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        Check = ReadRow(row);
                        if (Check != null)
                        {
                            Checks.Add(Check);
                        }
                    }
                }
                return Checks;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public CheckCollection Search(string CheckNumber, DateTime? dDate, int? BankId, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() {  CheckNumber, dDate,  BankId, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procCheckByFilterSelect", parameters);
                CheckCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as CheckCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }


    }
}
